package com.alameda.service.impl.user;

import com.alameda.common.base.enums.OperateResultEnum;
import com.alameda.common.base.enums.UserTypeEnum;
import com.alameda.common.utils.DateUtils;
import com.alameda.common.utils.ExcelUtils;
import com.alameda.common.utils.ObjectUtils;
import com.alameda.common.utils.PubUtils;
import com.alameda.mapper.user.AdUserOperateLogMapper;
import com.alameda.pojo.user.AdUserOperateLogDO;
import com.alameda.service.user.AdUserOperateLogService;
import com.alameda.user.bo.AdUserOperateLogBO;
import com.alameda.user.dto.AdUserOperateLogDTO;
import com.alameda.user.vo.AdUserOperateLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户操作日志 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2025/01/01
 */
@Slf4j
@Service
public class AdUserOperateLogServiceImpl extends ServiceImpl<AdUserOperateLogMapper, AdUserOperateLogDO> implements AdUserOperateLogService {

    @Autowired
    private AdUserOperateLogMapper adUserOperateLogMapper;

    /**
     * 读取用户操作信息
     *
     * @param adUserOperateLogDTO 用户操作日志信息
     * @return
     */
    @Override
    public IPage<AdUserOperateLogVO> listUserOperateInfo(AdUserOperateLogDTO adUserOperateLogDTO) {
        IPage<AdUserOperateLogDO> iPage = new Page<>(adUserOperateLogDTO.getPageNum(), adUserOperateLogDTO.getPageSize());
        LambdaQueryWrapper<AdUserOperateLogDO> lambdaQueryWrapper = getLambdaQueryWrapper(adUserOperateLogDTO);
        IPage<AdUserOperateLogDO> pageData = adUserOperateLogMapper.selectPage(iPage, lambdaQueryWrapper);
        return convertData(pageData);
    }

    /**
     * 数据模型转换 AdUserOperateLogDO -> AdUserOperateLogVO
     *
     * @param pageData 用户操作信息
     * @return
     */
    private IPage<AdUserOperateLogVO> convertData(IPage<AdUserOperateLogDO> pageData) {
        IPage<AdUserOperateLogVO> adUserOperateLogVoPage = ObjectUtils.convertPageData(pageData, AdUserOperateLogVO.class);
        adUserOperateLogVoPage.getRecords().forEach(item -> {
            item.setUserTypeEnum(UserTypeEnum.getByTypeCode(String.valueOf(item.getUserType())));
            item.setOperateResultEnum(OperateResultEnum.getByKey(item.getOperateApiResult()));
            item.setOperateApiTimeFormat(DateUtils.formatLocalDateTimeToYmdHms(item.getOperateApiTime()));
        });
        return adUserOperateLogVoPage;
    }

    /**
     * 导出excel
     *
     * @param adUserOperateLogDTO 用户操作日志信息
     */
    @Override
    public void exportExcel(AdUserOperateLogDTO adUserOperateLogDTO) {
        List<AdUserOperateLogBO> adUserOperateLogBOList = getExportData(adUserOperateLogDTO);
        ExcelUtils.export(adUserOperateLogBOList, AdUserOperateLogBO.class, "用户操作记录");
    }

    /**
     * 保存操作记录
     *
     * @param adUserOperateLogDTO 用户操作日志信息
     * @return
     */
    @Override
    public Boolean saveOperateLogInfo(AdUserOperateLogDTO adUserOperateLogDTO) {
        AdUserOperateLogDO adUserOperateLogDO = ObjectUtils.convert(adUserOperateLogDTO, AdUserOperateLogDO.class);
        return this.save(adUserOperateLogDO);
    }

    /**
     * 获取导出数据
     *
     * @param adUserOperateLogDTO 用户操作日志信息
     * @return
     */
    private List<AdUserOperateLogBO> getExportData(AdUserOperateLogDTO adUserOperateLogDTO) {
        if (PubUtils.isNotEmpty(adUserOperateLogDTO.getIdList())) {
            List<AdUserOperateLogDO> adUserOperateLogDOList = this.findByIdInOrderByCreateTimeDesc(adUserOperateLogDTO.getIdList());
            return ObjectUtils.convertList(adUserOperateLogDOList, AdUserOperateLogBO.class);
        } else {
            IPage<AdUserOperateLogVO> pageData = listUserOperateInfo(adUserOperateLogDTO);
            return ObjectUtils.convertList(pageData.getRecords(), AdUserOperateLogBO.class);
        }
    }

    /**
     * 获取构造查询条件
     *
     * @param adUserOperateLogDTO 用户操作日志信息
     * @return
     */
    private LambdaQueryWrapper<AdUserOperateLogDO> getLambdaQueryWrapper(AdUserOperateLogDTO adUserOperateLogDTO) {
        return Wrappers.lambdaQuery(AdUserOperateLogDO.class)
                .like(PubUtils.isNotEmpty(adUserOperateLogDTO.getUserName()), AdUserOperateLogDO::getUserName, adUserOperateLogDTO.getUserName())
                .eq(PubUtils.isNotNull(adUserOperateLogDTO.getUserType()), AdUserOperateLogDO::getUserType, adUserOperateLogDTO.getUserType())
                .like(PubUtils.isNotEmpty(adUserOperateLogDTO.getOperateApiName()), AdUserOperateLogDO::getOperateApiName, adUserOperateLogDTO.getOperateApiName())
                .eq(PubUtils.isNotEmpty(adUserOperateLogDTO.getOperateApiResult()), AdUserOperateLogDO::getOperateApiResult, adUserOperateLogDTO.getOperateApiResult())
                .like(PubUtils.isNotEmpty(adUserOperateLogDTO.getOperateApiTraceId()), AdUserOperateLogDO::getOperateApiTraceId, adUserOperateLogDTO.getOperateApiTraceId())
                .orderByDesc(AdUserOperateLogDO::getCreateTime);
    }
}
